﻿using BudgetManager.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using BudgetManager.Entities;namespace BudgetManager.Utils
{
    public class EmailHelper
    {
        public EmailEntity GetMailContentForExpenseReport(DateTime startDate, DateTime endDate)
        {
            EmailEntity entity = new EmailEntity();
            BudgetMgrDAL budgetmanager = new BudgetMgrDAL();
            List<Expense> data = budgetmanager.GetDetailedExpenses(startDate, endDate);
            entity.Subject = string.Format("Your Expense Statement for the period {0} to {1}", startDate.ToShortDateString(), endDate.ToShortDateString());
            StringBuilder builder = new StringBuilder();
            builder.AppendLine("Hi");
            builder.AppendLine("Please Find Below your expense Statement for the Period" + startDate.ToShortDateString() + " to " + endDate.ToShortDateString());
            builder.AppendLine("\n\n\n");
            builder.AppendLine(getContentFromData(data));
            builder.AppendLine("\n\n© MY BUDGET MANAGER");
            entity.Body = builder.ToString();
            return entity;
        }

        private string getContentFromData(List<Expense> data)
        {
            var total = data.Sum(e => e.ExpenseAmount);
            StringBuilder contentBuilder = new StringBuilder();
            contentBuilder.AppendLine("DATE\t\tAMOUNT\t\tCATEGORY\t\tCOMMENTS");
            foreach (var item in data)
            {
                contentBuilder.AppendLine(((DateTime)item.ExpenseDate).ToShortDateString() + "\t\t" + item.ExpenseAmount + "\t\t" + item.Category + "\t\t" + item.Comments);
            }
            contentBuilder.AppendLine("\n\n");
            contentBuilder.AppendLine("TOTAL EXPENDITURE: " + total);
            return contentBuilder.ToString();
        }
    }
}
